package com.yjw.service.impl;

import com.yjw.mapper.GoodinfoMapper;
import com.yjw.pojo.Goodinfo;
import com.yjw.pojo.Goods;
import com.yjw.mapper.GoodsMapper;
import com.yjw.pojo.Member;
import com.yjw.service.IGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yjw.service.IMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xq
 * @since 2025-05-15
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {

    @Resource
    IGoodsService goodsService;

    @Resource
    GoodinfoMapper goodinfoMapper;

    @Resource
    IMemberService memberService;

    @Override
    @Transactional
    public void sell(Goodinfo goodinfo) {
        // 1. 扣库存
        Goods goods = goodsService.getById(goodinfo.getGoodsid());
        goods.setInventory(goods.getInventory() - goodinfo.getCount());
        goodsService.updateById(goods);
        // 2. 减去会员的余额
        Member member = memberService.getById(goodinfo.getMemberid());
        member.setMemberbalance(member.getMemberbalance() - goodinfo.getPrice());
        memberService.updateById(member);
        // 3.新增消费记录
        goodinfoMapper.insert(goodinfo);
    }

}
